package com.whiteseason.springbootlean.controller;

import com.whiteseason.springbootlean.AjaxResponse;
import com.whiteseason.springbootlean.model.Article;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/rest")
public class ArticleController {
    //查询一篇文章，根据id
    //@RequestMapping(value = "/articles/{id}", method = RequestMethod.GET)
    @GetMapping("/articles/{id}")
    public AjaxResponse getArticle(@PathVariable("id") long id){
        Article article = Article.builder()
                .id(id)
                .author("whiteseason")
                .content("springboot 从青铜到王者")
                .createTime(new Date())
                .title("t1").build();
        log.info("article:" + article);
        return AjaxResponse.success(article);
    }

    //新增一篇文章
    //@RequestMapping(value = "/articles", method = RequestMethod.POST)
    @PostMapping("/articles")
    public AjaxResponse saveArticle(@RequestBody Article article){
        if (article.getId() == null){
            //TODO 抛出一个异常
        }
        log.info("saveArticle:" + article);
        return AjaxResponse.success();
    }

    //修改一篇文章
    //@RequestMapping(value = "/articles", method = RequestMethod.PUT)
    @PutMapping("/articles")
    public AjaxResponse updateArticle(@RequestBody Article article){
        if (article.getId() == null){
            //TODO 抛出一个异常
        }
        log.info("updateArticle:" + article);
        return AjaxResponse.success();
    }

    //删除一篇文章，根据id
    //@RequestMapping(value = "/articles/{id}", method = RequestMethod.DELETE)
    @DeleteMapping("/articles/{id}")
    public AjaxResponse deleteArticle(@PathVariable("id") long id){
        log.info("deleteArticle:" + id);
        return AjaxResponse.success();
    }
}
